﻿using AutoMapper;
using ManagementApi.Models.Manage.ProjectGroupMember;
using Modules;
using Modules.Attachment;
using Modules.Base;
using Modules.FaceScan;
using Modules.FaceScan.Entitys;
using Modules.Person;
using Modules.Project.Entitys;
using Modules.System.Attachment;
using Org.BouncyCastle.Utilities.Encoders;
using PmSoft;
using PmSoft.Exceptions;
using PmSoft.FileStorage;
using static Modules.Person.PersonConstants;

namespace ManagementApi.Models.Person
{
    public class PersonGroupMemberDto
    {
        /// <summary>
        /// 人员信息
        /// </summary>
        public PersonInfoDto Person { get; set; }

        /// <summary>
        /// 班组信息
        /// </summary>
        public ProjectGroupMemberEditForm Member { get; set; }

        /// <summary>
        /// 人员工资卡
        /// </summary>
        public PersonBankEditForm Bank { get; set; }

        /// <summary>
        /// 证书
        /// </summary>
        public IEnumerable<PersonCertificateForm> certificates { get; set; }
    }

    public static partial class PersonGroupMemberExceptions
    {
        /// <summary>
        /// 将member转换成人员详情 包含人员基础信息、附件
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public static PersonGroupMemberDto AsPersonDto(this ProjectGroupMember member)
        {
            PersonGroupMemberDto dto = new();
            var mapper = ServiceAccessor.GetService<IMapper>();

            #region 人员基础信息

            var personService = ServiceAccessor.GetService<PersonService>();
            var person = personService.GetPerson(member.PersonId);
            if (person == null) throw new BusinessException("人员信息不存在");

            PersonInfoDto personInfoDto = new();
            mapper.Map(person, personInfoDto);
            dto.Person = personInfoDto;

            var personFaceService = ServiceAccessor.GetService<FacePhotoService>();
            FacePhoto? personFace = personFaceService.GetPersonFaceByIdCard(person.CardNumber);
            if (personFace != null)
            {
                var fileStorage = ServiceAccessor.GetService<MinIoStorage>();
                byte[] objectData = fileStorage.GetFileAsync(FileBuckets.Person, personFace.IdCardPhoto).GetAwaiter().GetResult();
                if (objectData.Length > 0)
                    dto.Person.Base64PhotoStr = "data:image/png;base64," + Base64.ToBase64String(objectData);
            }

            #endregion 人员基础信息

            #region 班组信息

            ProjectGroupMemberEditForm memberForm = new();
            mapper.Map(member, memberForm);
            mapper.Map(member.AsProjectGroupMemberDto(), memberForm);
            dto.Member = memberForm;

            #endregion 班组信息

            #region 银行卡

            var personBankService = ServiceAccessor.GetService<PersonBankService>();
            PersonBank? personBank = personBankService.GetDefaultPersonBankByIdCard(person.CardNumber);
            if (personBank != null)
            {
                PersonBankEditForm bankForm = new();
                mapper.Map(personBank, bankForm);
                dto.Bank = bankForm;
            }

            #endregion 银行卡

            #region 证书及专业

            IEnumerable<PersonCertificateForm> certificatesForm = new List<PersonCertificateForm>();
            var personCertificateService = ServiceAccessor.GetService<PersonCertificateService>();
            IEnumerable<PersonCertificate> personCertificates = personCertificateService.GetPersonCertificatesByPersonId(person.PersonId);
            mapper.Map(personCertificates, certificatesForm);

            if (personCertificates.Any())
            {
                var personCertificateSpecialityService = ServiceAccessor.GetService<PersonCertificateSpecialityService>();
                IEnumerable<PersonCertificateSpeciality> personCertificateSpecialities = personCertificateSpecialityService.GetPersonCertificateSpecialitysByCetificteIds(personCertificates.Select(a => a.Id));

                var personAttachmentService = ServiceAccessor.GetService<PersonAttachmentService>();
                foreach (var item in certificatesForm)
                {
                    #region 专业

                    IEnumerable<PersonCertificateSpeciality> personCertificateSpecialityForms = personCertificateSpecialities.Where(a => a.CetificteId == item.Id);
                    mapper.Map(personCertificateSpecialityForms, item.Speciality);

                    #endregion 专业

                    #region 证书附件

                    IEnumerable<AttachmentInfo> attachments = personAttachmentService.GetSysAttachmentByObjectIdAndType(item.Id ?? string.Empty, 人员附件类型.证书附件, 人员附件租户.Certificate);
                    List<AttachmentsForm<string>> attach = new List<AttachmentsForm<string>>();
                    foreach (var attachment in attachments)
                    {
                        AttachmentsForm<string> attachmentsForm = new()
                        {
                            ObjectId = item.Id ?? string.Empty,
                            AttachmentId = attachment.Id,
                            AttachmentName = attachment.FriendlyName,
                        };
                        attach.Add(attachmentsForm);
                    }
                    item.Attachments = attach;

                    #endregion 证书附件
                }
            }

            dto.certificates = certificatesForm;

            #endregion 证书及专业

            return dto;
        }
    }
}